package com.dataextend.controller;

import com.dataextend.entity.*;
import com.dataextend.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "用户角色表接口")
@RestController
@RequestMapping("/api/user/role")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class RootUserRoleController extends RootBaseApiController {

    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("添加用户角色")
    @RequiresPermissions("user:role:add")
    @PostMapping("/add")
    public Map<String, Object> add(@RequestParam(value = "userId", required = false) Long userId,
                                   @RequestParam(value = "roleId", required = false) Long roleId)
    {
        UserRoleEntity userRoleEntity = new UserRoleEntity()
                .setUserId(userId)
                .setRoleId(roleId);

        if (userRoleService.add(userRoleEntity)==0) {
            return resp(406,"添加失败");
        } else {
            return resp(200, "添加成功", userRoleEntity);
        }
    }

    @ApiOperation("修改用户角色信息")
    @RequiresPermissions("user:role:update")
    @PostMapping("/update")
    public Map<String, Object> update(@RequestParam(value = "id", required = false) Long id,
                                      @RequestParam(value = "userId", required = false) Long userId,
                                      @RequestParam(value = "roleId", required = false) Long roleId) {
        UserRoleEntity userRoleEntity = new UserRoleEntity()
                .setId(id)
                .setUserId(userId)
                .setRoleId(roleId);

        return resp(200, "修改成功", userRoleService.update(userRoleEntity));
    }

    @ApiOperation("删除用户角色")
    @RequiresPermissions("user:role:delete")
    @PostMapping("/delete")
    public Map<String, Object> deleteById(@RequestParam(value = "id", required = false) Long id) {
        userRoleService.delete(id);
        return resp(200, "删除成功");
    }

    @ApiOperation("根据id查询用户角色信息")
    @GetMapping("/query")
    public Map<String, Object> queryById(@RequestParam(value = "id", required = false)  Long id) {
        return resp(200, "查询成功", userRoleService.queryById(id));
    }

    @ApiOperation("查询所有用户角色信息")
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam(value = "pageNum", required = false) int pageNum,
                                    @RequestParam(value = "pageSize", required = false) int pageSize,
                                    @RequestParam(value = "id", required = false) Long id,
                                    @RequestParam(value = "userId", required = false) Long userId,
                                    @RequestParam(value = "roleId", required = false) Long roleId) {
        UserRoleEntity userRoleEntity = new UserRoleEntity()
                .setId(id)
                .setUserId(userId)
                .setRoleId(roleId);

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id asc");
        List<UserRoleEntity> list = userRoleService.list(userRoleEntity);
        PageInfo<UserRoleEntity> pageInfo =  new PageInfo<UserRoleEntity>(list);
        return resp(200, "查询所有成功", pageInfo);
    }
}
